<?php
namespace app\admin\controller\system;
use app\common\controller\Admin;
use think\facade\Validate;
use think\Request;
use app\common\annotation\NodeAnnotation;
/**
 * Class Administrator
 * @package app\admin\controller\system
 * @NodeAnnotation(
 *     title="邮件板块",
 *     parent="admin/system.setting",
 *     is_menu=0,
 *     is_view=0
 * )
 */
class Mailer extends Admin
{

    /**
     * @NodeAnnotation(
     *     title="邮箱配置",
     *     parent="admin/system.mailer",
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function index(Request $request)
    {
        $emailSetting = get_option('smtp_setting');
        $this->assign([
            "mailer" => $emailSetting
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="邮箱保存并提交",
     *     parent="admin/system.mailer",
     *     MenuRoot="admin/system.setting",
     *     is_menu = 0,
     *     is_view = 0
     * )
     */
    public function savemailer(Request $request)
    {
        $post = array_map('trim', $this->request->post());

        if (in_array('', $post) && !empty($post['smtpsecure']))
        {
            $this->error("不能留空！");
        }
        set_option('smtp_setting', $post);
        $this->success("保存成功！");
    }

    /**
     * @NodeAnnotation(
     *     title="邮件模板",
     *     parent="admin/system.mailer",
     *     MenuRoot="admin/system.setting",
     *     is_menu = 0,
     *     is_view = 1
     * )
     */
    public function template()
    {
        $allowedTemplateKeys = ['verification_code'];

        $templateKey         = $this->request->param('template_key');

        if (empty($templateKey) || !in_array($templateKey, $allowedTemplateKeys))
        {
            $this->error('非法请求！');
        }
        $template = get_option('email_template_' . $templateKey);
        $this->assign($template);
        return $this->fetch('template_verification_code');
    }

    /**
     * @NodeAnnotation(
     *     title="邮件模板提交",
     *     parent="admin/system.mailer",
     *     MenuRoot="admin/system.setting",
     *     is_menu = 0,
     *     is_view = 0
     * )
     */
    public function templatePost()
    {
        $allowedTemplateKeys = ['verification_code'];
        $templateKey         = $this->request->param('template_key');

        if (empty($templateKey) || !in_array($templateKey, $allowedTemplateKeys)) {
            $this->error('非法请求！');
        }

        $data = $this->request->post();

        unset($data['template_key']);
        set_option('email_template_' . $templateKey, $data);

        $this->success("保存成功！");
    }

    /**
     * @NodeAnnotation(
     *     title="邮件发送测试",
     *     parent="admin/system.mailer",
     *     MenuRoot="admin/system.setting",
     *     is_menu = 0,
     *     is_view = 0
     * )
     */
    public function test()
    {
        if ($this->request->isPost()) {

            $validate = new \think\Validate([
                'to'      => 'require|email',
                'subject' => 'require',
                'content' => 'require',
            ]);
            $validate->message([
                'to.require'      => '收件箱不能为空！',
                'to.email'        => '收件箱格式不正确！',
                'subject.require' => '标题不能为空！',
                'content.require' => '内容不能为空！',
            ]);

            $data = $this->request->param();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $result = send_email($data['to'], $data['subject'], $data['content']);
            if ($result && empty($result['error'])) {
                $this->success('发送成功！');
            } else {
                $this->error('发送失败：' . $result['message']);
            }
        } else {
            return $this->fetch();
        }
    }
}